﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;


namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class UpdateItem : Form,IItemView
    {
        public event EventHandler OnUpdateComplete;

        private string questionId;
        private string itemId;
        private string projectId;

        public IItemController controller;

        public UpdateItem(string questionId, string itemId)
        {
            InitializeComponent();
            controller = new ItemController(this);
            this.questionId = questionId;
            this.itemId = itemId;
        }

        void Clear()
        {
            txtContent.Text = "";
            txtTitle.Text = "";
            cbDefault.Checked = false;
            txtImage.Text = string.Empty;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddItem_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnUpdateComplete(null, null);
        }

        public void CreateItemComplete(string message)
        {
            MessageBox.Show(message);
            Clear();
        }

        public void EditItemComplete(string message)
        {
            MessageBox.Show(message);
        }

        public void GetItemComplete(VT_Items item)
        {
            txtContent.Text = item.Item_Content;
            txtTitle.Text = item.Item_Title;
            cbDefault.Checked = (item.Item_IfDefault == 1 ? true : false);
            //cbProject.Checked = (!string.IsNullOrWhiteSpace(item.Project_ID));
            projectId = item.Project_ID;
            txtContent.Enabled = txtTitle.Enabled = btnImage.Enabled =  string.IsNullOrWhiteSpace(item.Project_ID);
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPEG文件(*.jpg)|*.jpg|PNG文件(*.png)|*.png|GIF文件(*.gif)|*.gif";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                txtImage.Text = filePath;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (cbProject.Checked && cbbProject.SelectedItem != null)
            //{
            //    VT_Projects project = cbbProject.SelectedItem as VT_Projects;
            //    controller.EditItem(
            //        questionId,
            //        project.Project_ID,
            //        itemId,
            //        txtTitle.Text,
            //        txtContent.Text,
            //        project.ProjectImage.Project_Image,
            //        (cbDefault.Checked ? 1 : 0));
            //}
            //else
            //{
            controller.EditItem(
              questionId,
              projectId,//界面不更新projectId,可支持非关联项目选项和已关联项目选项
              itemId,
              txtTitle.Text,
              txtContent.Text,
              txtImage.Text,
              (cbDefault.Checked ? 1 : 0));
            //}
        }

        private void UpdateItem_Load(object sender, EventArgs e)
        {
            controller.GetItem(questionId, itemId);
            controller.GetProjects();
        }

        private void cbbProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbProject.Checked && cbbProject.SelectedItem != null)
            {
                VT_Projects project = cbbProject.SelectedItem as VT_Projects;
                txtContent.Text = project.Project_Description;
                txtTitle.Text = project.Project_Name;
            }
        }

        private void cbProject_CheckedChanged(object sender, EventArgs e)
        {
            cbbProject.Visible = cbProject.Checked;

            txtContent.Enabled = !cbProject.Checked;
            txtTitle.Enabled = !cbProject.Checked;
            txtImage.Enabled = !cbProject.Checked;
            btnImage.Enabled = !cbProject.Checked;
        }


        public void GetProjectsComplete(IList<VT_Projects> projects)
        {
            cbbProject.DataSource = projects;
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }


        public void CreateItemsComplete(string message)
        {
            throw new NotImplementedException();
        }

        public void GetItemsComplete(IList<VT_Items> item)
        {
            throw new NotImplementedException();
        }
    }
}
